package com.thread;

import com.controller.CoreController;
import com.mapper.AccountMapper;
import com.model.AccessToken;
import com.model.account.Account;
import com.service.account.AccountServiceImpl;
import com.util.WeixinUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 用单线程去获取access_token
 */
public class AccessTokenThread extends Thread {
    private static Logger log = LoggerFactory.getLogger(CoreController.class);
    GetAccount getAccount=new GetAccount();
    //公众号id
    public static String accountid;
    // 第三方用户唯一凭证
    public  String appid= AccountServiceImpl.appid;
    // 第三方用户唯一凭证密钥
    public  String appsecret= AccountServiceImpl.appsecret;
    public static AccessToken accessToken ;
    public void run() {

        while (true) {
            accessToken = WeixinUtil.getAccessToken(appid, appsecret);

            if (null != accessToken) {

                try {
                    log.info("获取成功，accessToken:" + accessToken.getToken());
                    Thread.sleep(7200000);    // 休眠7200秒
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            } else {
                try {
                    log.error("获取token失败");
                    Thread.sleep(60000);    // 休眠60秒
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }

    }
public static void getAccountid(String id){
    accountid=id;
}

}
